package co.chonghao.ch_kuaichong_user.ui.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import co.chonghao.ch_kuaichong_user.R;
import co.chonghao.ch_kuaichong_user.adapter.HomeAdapter;
import co.chonghao.ch_kuaichong_user.bean.FragmentInfo;
import co.chonghao.ch_kuaichong_user.utils.TitleBuilder;

/**
 * 描述：首页
 * Created by admin on 2017/7/31.
 */

public class HomeFragment extends Fragment {
    @BindView(R.id.tab_home_titles_layout)
    TabLayout tabHomeTitlesLayout;
    @BindView(R.id.vp_show_layout)
    ViewPager vpShowLayout;
    Unbinder unbinder;
    private int mSelectedColor;
    private List<FragmentInfo> mShowItems = new ArrayList<>();
    private String [] title = {"固定桩","移动服务桩","救援"};
    private int [] tab_bg = {R.drawable.tab_bg_guding,R.drawable.tab_bg_yidong,R.drawable.tab_bg_jiuyuan};
    private int [] tab_icon = {R.drawable.tab_icon_guding,R.drawable.tab_icon_yidong,R.drawable.tab_icon_jiuyuan};
    private int [] tabWidth = {124,124,117};
    private HomeAdapter homeAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, null);
        unbinder = ButterKnife.bind(this, view);
        new TitleBuilder(view).setMiddleTitleText("").build();
        init();
        return view;
    }

    private void init() {
        if (mShowItems.size() == 0) {
            mShowItems.add(new FragmentInfo(new Stationary_Charging_Fragment(),title[0]));
            mShowItems.add(new FragmentInfo(new MobileChrging_Fragment(),title[1]));
            mShowItems.add(new FragmentInfo(new Help_Charging_Fragment(),title[2]));
        }
        //初始化viewpager
        homeAdapter = new HomeAdapter(getChildFragmentManager(), mShowItems);
        vpShowLayout.setAdapter(homeAdapter);

        //页签与viewpager绑定
        tabHomeTitlesLayout.setupWithViewPager(vpShowLayout);

        for (int i = 0; i < homeAdapter.getCount(); i++) {
            TabLayout.Tab tab = tabHomeTitlesLayout.getTabAt(i);//获得每一个tab
            View customview = LayoutInflater.from(getContext()).inflate(R.layout.tab_item, null);
            tab.setCustomView(customview);//给每一个tab设置view
            if (i == 0) {
                // 设置第一个tab的TextView是被选择的样式
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(true);//第一个tab被选中
            }
            TextView textView = (TextView) tab.getCustomView().findViewById(R.id.tab_text);
            final LinearLayout ll_tab = (LinearLayout) tab.getCustomView().findViewById(R.id.ll_tab);


            ll_tab.setBackgroundResource(tab_bg[i]);//设置tab背景



            ImageView iv_icon = (ImageView) tab.getCustomView().findViewById(R.id.iv_icon);

            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    tabWidth[i],
                    LinearLayout.LayoutParams.MATCH_PARENT
            );
            tab.getCustomView().setLayoutParams(p);
            textView.setText(title[i]);//设置tab上的文字
            textView.setTextColor(getResources().getColor(R.color.text_orange));
            iv_icon.setImageResource(tab_icon[i]);
        }
        tabHomeTitlesLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(true);
                vpShowLayout.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.getCustomView().findViewById(R.id.tab_text).setSelected(false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        //设置tablayout的颜色
        //int normalColor = getResources().getColor(R.color.text_nor);
        //mSelectedColor = getResources().getColor(R.color.text_pre);
        //tabHomeTitlesLayout.setTabTextColors(normalColor, mSelectedColor);

        //设置tablayout底部导航的颜色
        tabHomeTitlesLayout.setSelectedTabIndicatorColor(mSelectedColor);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onResume() {
        super.onResume();

    }
}
